home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / computerjanitorapp / ui_cli_tests.py < prev    next >
Encoding:
Python Source  |  2009-03-04  |  7.2 KB  |  247 lines

  1. # ui_cli_tests.py - unit tests for ui_cli.py
  2. # Copyright (C) 2008, 2009  Canonical, Ltd.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, version 3 of the License.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15.  
  16.  
  17. import unittest
  18.  
  19. import computerjanitor
  20. import computerjanitorapp
  21.  
  22.  
  23. class MockCruft(object):
  24.  
  25.     def __init__(self, name):
  26.         self._name = name
  27.         self.cleaned = False
  28.         self.post_cleaned = False
  29.         
  30.     def get_name(self):
  31.         return self._name
  32.  
  33.     def cleanup(self):
  34.         self.cleaned = True
  35.  
  36.  
  37. class MockPlugin(object):
  38.  
  39.     def __init__(self, crufts):
  40.         self._crufts = crufts
  41.         self.post_cleaned = False
  42.         
  43.     def get_cruft(self):
  44.         for cruft in self._crufts:
  45.             yield cruft
  46.         
  47.     def post_cleanup(self):
  48.         self.post_cleaned = True
  49.             
  50.  
  51. class MockPluginManager(object):
  52.  
  53.     def __init__(self, plugins):
  54.         self._plugins = plugins
  55.         
  56.     def get_plugins(self):
  57.         return self._plugins
  58.  
  59.  
  60. class MockState(object):
  61.  
  62.     def __init__(self, enabled):
  63.         self._enabled = set(enabled)
  64.  
  65.     def is_enabled(self, name):
  66.         return name in self._enabled
  67.  
  68.     def enable(self, name):
  69.         if name not in self._enabled:
  70.             self._enabled.add(name)
  71.  
  72.     def disable(self, name):
  73.         if name in self._enabled:
  74.             self._enabled.remove(name)
  75.  
  76.     def load(self, filename):
  77.         self.load_filename = filename
  78.  
  79.     def save(self, filename):
  80.         self.save_filename = filename
  81.  
  82.  
  83. class MockOptionParser(object):
  84.  
  85.     def __init__(self):
  86.         self.help_printed = False
  87.         
  88.     def print_help(self):
  89.         self.help_printed = True
  90.  
  91.  
  92. class MockApplication(object):
  93.  
  94.     def __init__(self, enabled):
  95.         self.state = MockState(enabled)
  96.         self.parser = MockOptionParser()
  97.  
  98.     def verify_apt_cache(self):
  99.         pass
  100.  
  101.     def remove_whitelisted(self, crufts):
  102.         return crufts
  103.  
  104.  
  105. class MockOptions(object):
  106.  
  107.     def __init__(self):
  108.         self.all = None
  109.         self.state_file = "foo"
  110.         self.no_act = None
  111.         self.verbose = None
  112.  
  113.  
  114. class CommandLineUserInterfaceTests(unittest.TestCase):
  115.  
  116.     def setUp(self):
  117.         self.app = MockApplication(["foo"])
  118.         self.cruft_names = ["foo", "bar"]
  119.         self.crufts = [MockCruft(name) for name in self.cruft_names]
  120.         self.cruftdict = dict((c.get_name(), c) for c in self.crufts)
  121.         self.plugin = MockPlugin(self.crufts)
  122.         self.pm = MockPluginManager([self.plugin])
  123.         self.ui = computerjanitorapp.CommandLineUserInterface(self.app, 
  124.                                           self.pm, mustberoot=False)
  125.  
  126.         self.options = MockOptions()
  127.  
  128.     def testInsistsOnBeingRoot(self):
  129.         self.ui.mustberoot = True
  130.         self.assertRaises(computerjanitor.Exception, self.ui.run, None, 
  131.                           None)
  132.  
  133.     def testFindsTheRightCruft(self):
  134.         self.assertEqual(self.ui.find_cruft(), self.crufts)
  135.  
  136.     def testShowsTheRightCruftTheRightWay(self):
  137.  
  138.         def mock_find_cruft():
  139.             return self.crufts
  140.  
  141.         def mock_show_one_cruft(name, desc, state, width):
  142.             output.append((name, state))
  143.  
  144.         output = []
  145.         self.ui.find_cruft = mock_find_cruft
  146.         self.ui.show_one_cruft = mock_show_one_cruft
  147.         self.ui.show_cruft(MockOptions(), None)
  148.         self.assertEqual(output, 
  149.                          sorted([("foo", "removable"), ("bar", "ignored")]))
  150.  
  151.     def testIgnoresCruftCorrectly(self):
  152.         self.ui.ignore(self.options, ["foo"])
  153.         self.assertFalse(self.app.state.is_enabled("foo"))
  154.  
  155.     def testUnignoresCruftCorrectly(self):
  156.         self.ui.unignore(self.options, ["bar"])
  157.         self.assert_(self.app.state.is_enabled("bar"))
  158.  
  159.     def testCleansUpEnabledCruftWithDashDashAll(self):
  160.         self.options.all = True
  161.         self.ui.cleanup(self.options, [])
  162.         self.assert_(self.cruftdict["foo"].cleaned)
  163.  
  164.     def testDoesNotCleanUpEnabledCruftWithDashDashAllWhenNoActIsSet(self):
  165.         self.options.all = True
  166.         self.options.no_act = True
  167.         self.ui.cleanup(self.options, [])
  168.         self.assertFalse(self.cruftdict["foo"].cleaned)
  169.  
  170.     def testCleansUpRequestedEnabledCruft(self):
  171.         self.ui.cleanup(self.options, ["foo"])
  172.         self.assert_(self.cruftdict["foo"].cleaned)
  173.  
  174.     def testDoesNotCleanUpDisaabledCruftWithDashDashAll(self):
  175.         self.options.all = True
  176.         self.ui.cleanup(self.options, [])
  177.         self.assertFalse(self.cruftdict["bar"].cleaned)
  178.  
  179.     def testCleansUpRequestedDisabledCruft(self):
  180.         self.ui.cleanup(self.options, ["bar"])
  181.         self.assert_(self.cruftdict["bar"].cleaned)
  182.  
  183.     def testRunsPostCleanup(self):
  184.         self.ui.cleanup(self.options, [])
  185.         self.assert_(self.plugin.post_cleaned)
  186.  
  187.     def testDoesNotRunPostCleanupWhenNoActIsSet(self):
  188.         self.options.no_act = True
  189.         self.ui.cleanup(self.options, [])
  190.         self.assertFalse(self.plugin.post_cleaned)
  191.  
  192.     def testRaisesExceptionForUnknownCruft(self):
  193.         self.assertRaises(computerjanitor.Exception, self.ui.cleanup,
  194.                           self.options, ["unknown"])
  195.  
  196.     def testHelpCallsParserPrintHelp(self):
  197.         self.ui.help(None, None)
  198.         self.assert_(self.app.parser.help_printed)
  199.  
  200.     def setup_run(self):
  201.         names = ["show_cruft", "cleanup", "ignore", "unignore", "help"]
  202.         for name in names:
  203.             method = getattr(self.ui, name)
  204.             wrapper = lambda options, args, name=name: \
  205.                         setattr(self, "operation", name)
  206.             setattr(self.ui, name, wrapper)
  207.  
  208.     def testRunCallsShowCruft(self):
  209.         self.setup_run()
  210.         self.ui.run(self.options, ["find"])
  211.         self.assertEqual(self.operation, "show_cruft")
  212.  
  213.     def testRunCallsCleanup(self):
  214.         self.setup_run()
  215.         self.ui.run(self.options, ["cleanup"])
  216.         self.assertEqual(self.operation, "cleanup")
  217.  
  218.     def testRunCallsIgnore(self):
  219.         self.setup_run()
  220.         self.ui.run(self.options, ["ignore"])
  221.         self.assertEqual(self.operation, "ignore")
  222.  
  223.     def testRunCallsUnignore(self):
  224.         self.setup_run()
  225.         self.ui.run(self.options, ["unignore"])
  226.         self.assertEqual(self.operation, "unignore")
  227.  
  228.     def testRunCallsHelp(self):
  229.         self.setup_run()
  230.         self.ui.run(self.options, ["help"])
  231.         self.assertEqual(self.operation, "help")
  232.  
  233.     def testRunCallsHelpWhenThereAreNoArguments(self):
  234.         self.setup_run()
  235.         self.ui.run(self.options, [])
  236.         self.assertEqual(self.operation, "help")
  237.  
  238.     def testRaisesExceptionForUnknownCommand(self):
  239.         self.assertRaises(computerjanitor.Exception, self.ui.run, 
  240.                           self.options, ["yikes"])
  241.  
  242.     def testRunLoadsStateFromRightFile(self):
  243.         self.setup_run()
  244.         self.ui.run(self.options, ["ignore"])
  245.         self.assertEqual(self.app.state.load_filename, 
  246.                          self.options.state_file)
  247.